#include "gd32f30x.h"
#include "string.h"
#include "stdint.h"
#include "stdio.h"
#include "DevicesQueue.h"
#include "DevicesUart.h"
#include "DevicesCAN.h"
#include "taskMessage.h"


/* 任务句柄 */
MrTaskType *g_ptypeMrTaskMessage;

/* 消息队列句柄 */
MrMessageType *g_ptypeMrMessageNetwork;


/* 解析缓存长度 */
#define MESSAGE_BUFF_LENGTH 256
/* 解析缓存 */
static uint8_t st_ucMessageBuff[MESSAGE_BUFF_LENGTH];


void vMrTaskMessage(uint32_t uiParameters)
{
    int32_t iLength = 0;

    cMrMessageCreate(&g_ptypeMrMessageNetwork,  MR_MESSAGE_TYPE_FIFO, sizeof(uint32_t), 8, 0);

    while(1)
    {
        cMessageRead(g_ptypeMrMessageNetwork, &uiParameters, -1);

        /* 读取并解析 上位机 发送过来的数据 */
        while((iLength = iQueueGetLengthOfOccupy(&g_TypeQueueUart1Read)) != 0)
        {
            iLength = (iLength > sizeof(st_ucMessageBuff)) ? sizeof(st_ucMessageBuff) : iLength;

            memset(st_ucMessageBuff, 0, sizeof(st_ucMessageBuff));
            enumQueuePopDatas(&g_TypeQueueUart1Read, st_ucMessageBuff, iLength);

            printf("Uart1Read: %s\r\n", st_ucMessageBuff);

            vUartDMASendDatas(USART1, st_ucMessageBuff, iLength);
        }
    }
}
